x
import pandas as pd#find dataset data = pd.read_csv(r"C:\Users\cbenj\OneDrive\Desktop\School\AdvanceData\Data Comparison\owid-covid-data.csv")# Filter data for the years 2020 to 2023data = data[(data['date'] >= '2020-01-01') & (data['date'] <= '2023-12-31')]# Group the data by "location" (country) and "date"grouped_data = data.groupby(['location', 'date'])# Calculate the sum of "total deaths," "total vaccinations," and "total cases" for each country and datesummary_data = grouped_data[['total_deaths', 'total_vaccinations', 'total_cases', 'population']].sum().reset_index()# Save the summary data to a new CSV filesummary_data.to_csv('summary_data.csv', index=False)x
data['date'] = pd.to_datetime(data['date'])xxxxxxxxxximport matplotlib.pyplot as pltfrom matplotlib.ticker import MaxNLocatorx
continent = data['location'].unique()plt.figure(figsize=(12,6))for location in continent: country_data = data[data['location'] == location] plt.plot(country_data['date'], country_data['total_deaths'], label=location)plt.xlabel('Date')plt.ylabel('Total Deaths')plt.title('Time series of total deaths')plt.legend(loc='upper left', bbox_to_anchor=(1,1))plt.show()x
plt.figure(figsize=(12, 6))for location in continent: country_data = data[data['location'] == location] plt.plot(country_data['date'], country_data['total_vaccinations'], label=location)plt.xlabel('Date')plt.ylabel('Total Vaccinated')plt.title('Time series of total vaccinated')plt.legend(loc='upper left', bbox_to_anchor=(1, 1))plt.show()xxxxxxxxxximport plotly.graph_objs as goimport plotly.subplots as spfrom plotly.subplots import make_subplotsimport ipywidgets as widgetsfrom IPython.display import displayfrom IPython.display import clear_output# Create a function to update the chart based on the selected datadef update_chart(selected_location, selected_data): data_subset = data[data['location'] == selected_location] data_column = 'total_deaths' if selected_data == 'total_deaths' else 'total_vaccinations' fig = make_subplots(rows=1, cols=1) trace = go.Scatter(x=data_subset['date'], y=data_subset[data_column], mode='lines', name=selected_data) fig.add_trace(trace) fig.update_xaxes(title='Date') fig.update_yaxes(title=selected_data) fig.update_layout(title=f'{selected_data} Over Time') return fig# Dropdown widget for selecting locationlocation_dropdown = widgets.Dropdown(options=data['location'].unique(), description='Location:')# Radio buttons widget for selecting data typedata_radio = widgets.RadioButtons(options=['total_deaths', 'total_vaccinations'], description='Data Type:')# Function to update the chart when widgets changedef on_change(change): if change['name'] == 'value': clear_output() display(location_dropdown, data_radio) display(update_chart(location_dropdown.value, data_radio.value))location_dropdown.observe(on_change)data_radio.observe(on_change)# Initial displaydisplay(location_dropdown, data_radio)display(update_chart(location_dropdown.value, data_radio.value))# Create subplotsfig = sp.make_subplots(rows=1, cols=2, shared_xaxes=True)# Add the total_deaths tracetrace_deaths = go.Scatter(x=data['date'], y=data['total_deaths'], name='Total Deaths')fig.add_trace(trace_deaths, row=1, col=1)# Add the total_vaccinated trace (corrected column name)trace_vaccinated = go.Scatter(x=data['date'], y=data['total_vaccinations'], name='Total Vaccinated')fig.add_trace(trace_vaccinated, row=1, col=2)# Update layout for the subplotsfig.layout.update( title='Total Deaths and Total Vaccinated Over Time', xaxis=dict(title='Date'), yaxis=dict(title='Counts'))# Create updatemenuupdatemenus = [ dict( buttons=list([ dict(label='Total Deaths', method='relayout', args=['xaxis.title.text', 'Date'], args2=[{'xaxis.title.text': 'Total Deaths'}]), dict(label='Total Vaccinated', method='relayout', args=['xaxis.title.text', 'Date'], args2=[{'xaxis.title.text': 'Total Vaccinated'}]) ]), direction='down', showactive=True )]# Add updatemenu to the layoutfig.layout.updatemenus = updatemenusfig.show()